#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define XH(i,a,b) for(int i = a; i < b; ++i)
#define XXH(i,b,a) for(int i = b; i >= a; --i)
#define Module 998244353
int tmpInt;LL tmpLL;string tmpStr;char tmpChar;
vector<int>tmpIntVector;

LL Fun(LL num){
  vector<int>v;
  int cnt = 0;
  while(num>0){
    v.push_back(num%10);
    num /= 10;
  }
  sort(v.begin(),v.end());
  LL num1 = 0, num2 = 0;
  int size = v.size();
  XH(i,0,size){
    num1 = num1 * 10 + v[size - i -1];
    num2 = num2 *10 + v[i];
  }
  return num1 - num2;
}

int main(){
  LL N,K;
  cin>>N>>K;
  LL cnt = 0;
  while(cnt<K){
    cnt++;
    N  = Fun(N);
    if(N == 6174)break;
  }
  cout<<N<<endl;

  return 0;
}
